<template>
  <!-- 制芯基础数据表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="600"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="80">
      <el-row>
        <el-col :span="12">
          <el-form-item label="产品编号">
            <el-input v-model="formData.materialCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品名称">
            <el-input v-model="formData.materialName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="砂芯编号" prop="sandCoreNumber">
            <el-input v-model="formData.sandCoreNumber" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="件/M" prop="pm">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.pm"
              placeholder="请输入件/M"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="M/S" prop="ms">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.ms"
              placeholder="请输入M/S"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划停机" prop="scheduledOutage">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.scheduledOutage"
              placeholder="请输入计划停机"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备停机" prop="equipmentShutdown">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipmentShutdown"
              placeholder="请输入设备停机"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班制" prop="shiftSet">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.shiftSet"
              placeholder="请输入班制"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班产" prop="tactYield">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.tactYield"
              placeholder="请输入班产"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="冷/热" prop="coreState">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.coreState"
              placeholder="请输入冷芯热芯状态"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备型号" prop="equipModel">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipModel"
              placeholder="请输入设备型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="砂型号" prop="sandModel">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.sandModel"
              placeholder="请输入砂型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="需求人数" prop="demandPeople">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.demandPeople"
              placeholder="请输入需求人数"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="砂芯物料" prop="sandCoreMaterialCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.sandCoreMaterialCode"
              placeholder="请输入砂芯物料编号并用,分割"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESCOREBASICDATAADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sandCoreNumber: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  pm: [
    { validator: validateNumber, trigger: "blur" },
    { min: 1, max: 9, message: "长度在 1 到 9 个数字", trigger: "blur" },
  ],
  ms: [
    { validator: validateNumber, trigger: "blur" },
    { min: 1, max: 9, message: "长度在 1 到 9 个数字", trigger: "blur" },
  ],
  equipmentShutdown: [
    { validator: validateNumber, trigger: "blur" },
    { min: 1, max: 9, message: "长度在 1 到 9 个数字", trigger: "blur" },
  ],
  scheduledOutage: [
    { validator: validateNumber, trigger: "blur" },
    { min: 1, max: 9, message: "长度在 1 到 9 个数字", trigger: "blur" },
  ],
  shiftSet: [],
  coreState: [],
  equipModel: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sandModel: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  demandPeople: [
    { validator: validateNumber, trigger: "blur" },
    { min: 1, max: 9, message: "长度在 1 到 9 个数字", trigger: "blur" },
  ],
  tactYield: [
    { validator: validateNumber, trigger: "blur" },
    { min: 1, max: 9, message: "长度在 1 到 9 个数字", trigger: "blur" },
  ],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  sandCoreMaterialCode: [
    { required: true, message: "请输入砂芯物料编号并用,分割", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  organizationName: "",
  plantCode: "",
  plantName: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  sandCoreNumber: "",
  pm: "",
  ms: "",
  scheduledOutage: "",
  equipmentShutdown: "",
  shiftSet: "",
  tactYield: "",
  coreState: "",
  equipModel: "",
  sandModel: "",
  demandPeople: "",
  sandCoreMaterialCode: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        organizationId,
        organizationCode,
        plantId,
        organizationName,
        plantCode,
        plantName,
        materialId,
        materialCode,
        materialName,
        sandCoreNumber,
        pm,
        ms,
        scheduledOutage,
        equipmentShutdown,
        shiftSet,
        tactYield,
        coreState,
        equipModel,
        sandModel,
        demandPeople,
        sandCoreMaterialCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        organizationId,
        organizationCode,
        plantId,
        organizationName,
        plantCode,
        plantName,
        materialId,
        materialCode,
        materialName,
        sandCoreNumber,
        pm,
        ms,
        scheduledOutage,
        equipmentShutdown,
        shiftSet,
        tactYield,
        coreState,
        equipModel,
        sandModel,
        demandPeople,
        sandCoreMaterialCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
      formData.tactYield = String(formData.tactYield);
      formData.pm = String(formData.pm);
      formData.ms = String(formData.ms);
      formData.scheduledOutage = String(formData.scheduledOutage);
      formData.equipmentShutdown = String(formData.equipmentShutdown);
      formData.demandPeople = String(formData.demandPeople);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.organizationName = "";
  formData.plantCode = "";
  formData.plantName = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.materialName = "";
  formData.sandCoreNumber = "";
  formData.pm = "";
  formData.ms = "";
  formData.scheduledOutage = "";
  formData.equipmentShutdown = "";
  formData.shiftSet = "";
  formData.tactYield = "";
  formData.coreState = "";
  formData.equipModel = "";
  formData.sandModel = "";
  formData.demandPeople = "";
  formData.sandCoreMaterialCode = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 制芯基础数据保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mescorebasicdata/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
function validateNumber(rule: any, value: any, callback: (a?: any) => {}) {
  if (isNaN(parseFloat(value)) && isFinite(value)) {
    callback(new Error("请输入数字"));
  } else if (!/^[+-]?\d*(\.\d*)?(e[+-]?\d+)?$/.test(value)) {
    callback(new Error("请输入数字"));
  } else if (Number(value) <= 0) {
    callback(new Error("请输入大于0的值"));
  } else if (!/^[+-]?\d+?(e[+-]?\d+)?$/.test(value)) {
    callback(new Error("请输入整数"));
  } else {
    callback();
  }
}
</script>
